package tcpTest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	private static int count = 0;

	private static ServerSocket server;

	public static void main(String[] args) throws IOException {
		// 建立服务器
		server = new ServerSocket(8888);
		boolean isRunning = true;
		while (isRunning) {
			new Thread(new Mychanel()).start();
		}
	}

	static class Mychanel implements Runnable {

		public Mychanel() {
			// TODO Auto-generated constructor stub
		}

		@Override
		public void run() {
			try {
				Task();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		private void Task() throws IOException {
			// 连接客户端
			Socket client = server.accept();

			// 2.取得IO
			InputStream bis = client.getInputStream();
			OutputStream bos = new FileOutputStream("bbb" + count + ".jpg");
			count++;

			// 3.写入数据
			byte[] buf = new byte[1024];
			int len = -1;
			while ((len = bis.read(buf)) != -1) {
				System.out.println(new String(buf));
				System.out.println("-----------------------------");
				bos.write(buf, 0, len);
			}
			bos.flush();

			bos.close();
			bis.close();

			// 关闭连接
			client.close();
		}
	}
}

class App {
	public static void main(String[] args) {
		System.out.println("hello world");
	}
}
